﻿using System;
using System.Web.Caching;
using Xpedite.Resources;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.WebAbstraction
{
  internal interface IWebCache
  {
    IResourceContainer Get(String key);
    void AddWithNoDependency(String key, IResourceContainer value);
    void AddWithFileDependency(String key, IResourceContainer value, String filename);
    void AddWithFileDependencies(String key, IResourceContainer value, String[] filenames);
    void AddWithCacheDependencies(String key, IResourceContainer value, String[] cacheKeys);
    void Remove(String key);
  }

  internal class WebCache : IWebCache
  {
    private readonly TimeSpan _slidingExpiration = TimeSpan.FromMinutes(60);
    private readonly String[] NoFileDependency = new String[0];
    private readonly Cache _cache;

    public WebCache(Cache cache)
    {
      Verify.NotNull(cache, "cache");

      _cache = cache;
    }

    public IResourceContainer Get(String key)
    {
      return _cache.Get(key) as IResourceContainer;
    }

    public void AddWithNoDependency(String key, IResourceContainer value)
    {
      Verify.NotWhiteSpace(key, "key");
      Verify.NotNull(value, "value");

      _cache.Add(key, value, null, Cache.NoAbsoluteExpiration, _slidingExpiration, CacheItemPriority.Default, OnCachedItemRemoved);
    }

    public void AddWithFileDependency(String key, IResourceContainer value, String filename)
    {
      Verify.NotWhiteSpace(filename, "filename");
      Verify.NotWhiteSpace(key, "key");
      Verify.NotNull(value, "value");

      _cache.Add(key, value, new CacheDependency(filename), Cache.NoAbsoluteExpiration, _slidingExpiration, CacheItemPriority.Default, OnCachedItemRemoved);
    }

    public void AddWithFileDependencies(String key, IResourceContainer value, String[] filenames)
    {
      Verify.NotNull(filenames, "filenames");
      Verify.NotWhiteSpace(key, "key");
      Verify.NotNull(value, "value");

      _cache.Add(key, value, new CacheDependency(filenames), Cache.NoAbsoluteExpiration, _slidingExpiration, CacheItemPriority.Default, OnCachedItemRemoved);
    }

    public void AddWithCacheDependencies(String key, IResourceContainer value, String[] cacheKeys)
    {
      Verify.NotNull(cacheKeys, "cacheKeys");
      Verify.NotWhiteSpace(key, "key");
      Verify.NotNull(value, "value");

      _cache.Add(key, value, new CacheDependency(NoFileDependency, cacheKeys), Cache.NoAbsoluteExpiration, _slidingExpiration, CacheItemPriority.Default, OnCachedItemRemoved);
    }

    public void Remove(String key)
    {
      _cache.Remove(key);
    }

    private void OnCachedItemRemoved(String key, Object value, CacheItemRemovedReason reason)
    {
      var disposable = value as IDisposable;

      if (disposable != null)
        disposable.Dispose();
    }
  }
}
